package commandAction;

import java.awt.Component;
import java.awt.Container;
import java.awt.KeyboardFocusManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;

import plugin.Activator;
import robotCross.TextMatcher;
import robotCross.TipMatcher;
import abbot.finder.BasicFinder;
import abbot.finder.ComponentNotFoundException;
import abbot.finder.MultipleComponentsFoundException;

import com.supermap.desktop.Application;
import com.supermap.desktop.Interface.IBaseItem;
import com.supermap.desktop.Interface.IToolbar;
import com.supermap.desktop.enums.WindowType;
import com.supermap.desktop.implement.SmButtonDropdown;
import com.supermap.desktop.implement.SmPopupMenu;
import com.supermap.desktop.ui.ToolbarManager;

import command.Command;
import command.SearchMap;
import command.SearchType;

public class ActionToolbar extends Action {
	JButton arrowButton = null;// menu的下拉三角按钮
	Object item = null;

	@Override
	public Object action(Command cmd) {
		// TODO Auto-generated method stub
		// 根据searchMap的条件查找控件
		Iterator<SearchMap> iterator = cmd.getSearchMap().iterator();
		if (iterator.hasNext()) {
			SearchMap map = iterator.next();
			// 暂时统一用tip找button和menu，button的label获取不到，menu应该可以用text找，未实现。
			item = findItemOnToolbar(map.getType(), (String) map.getValue());
		}
		// 默认点击
		if (cmd.getAction() == null || cmd.getAction().length == 0) {
			if (arrowButton != null) {
				robot.click(arrowButton);
				robot.delay(1000);
			}
			if (item != null) {
				robot.click((Component) item);
			}
		}
		return item;
	}

	// 根据tip查找工具条按钮,根据tip或者text菜单
	public Object findItemOnToolbar(SearchType type, String tip) {
		ToolbarManager toolbarManager = (ToolbarManager) Application
				.getActiveApplication().getMainFrame().getToolbarManager();

		// 桌面初始的toolbar,如"标注"
		for (int toolbarIndex = 0; toolbarIndex < toolbarManager.getCount(); toolbarIndex++) {
			IToolbar toolbar = toolbarManager.get(toolbarIndex);
			item = findBarItem(toolbar, type, tip);
			if (item != null)
				return item;
		}

		// 窗口的toolbar,如打开地图窗口之后的“对象绘制”
		if (Application.getActiveApplication().getActiveForm() != null) {
			WindowType windowType = Application.getActiveApplication()
					.getActiveForm().getWindowType();
			for (int toolbarIndex = 0; toolbarIndex < toolbarManager
					.getChildToolbarCount(windowType); toolbarIndex++) {
				IToolbar toolbar = toolbarManager.getChildToolbar(windowType,
						toolbarIndex);
				item = findBarItem(toolbar, type, tip);
			}
		}
		return item;
	}

	// 根据给定的toolbar查找按钮或menuItem
	public Object findBarItem(IToolbar toolbar, SearchType type, String tip) {
		Object item1 = null;
		/*
		 * 用BasicFinder不好查找执行下拉菜单选项，后面再看看，可能需要传dropdown button的text和菜单项的text if
		 * (type.equals(SearchType.TIP)) { try { item1 = new
		 * BasicFinder().find((Container) toolbar, new TipMatcher(tip)); } catch
		 * (ComponentNotFoundException | MultipleComponentsFoundException e) {
		 * // TODO Auto-generated catch block e.printStackTrace(); } } else if
		 * (type.equals(SearchType.TEXT)) { try { item1 = new
		 * BasicFinder().find((Container) toolbar, new TextMatcher(tip)); }
		 * catch (ComponentNotFoundException | MultipleComponentsFoundException
		 * e) { // TODO Auto-generated catch block e.printStackTrace(); } }
		 */
		for (int itemIndex = 0; itemIndex < toolbar.getCount(); itemIndex++) {

			item1 = toolbar.getAt(itemIndex);
			// 按钮
			if (item1 instanceof JButton
					&& tip.equals(((JButton) item1).getToolTipText())) {
				return (JButton) item1;
			}
			// 下拉菜单
			else if (item1 instanceof SmButtonDropdown) {
				SmButtonDropdown dropdown = (SmButtonDropdown) item1;
				Method method;
				try {
					method = dropdown.getClass().getDeclaredMethod(
							"getPopupMenu");
					method.setAccessible(true);
					SmPopupMenu menu = (SmPopupMenu) method.invoke(dropdown);
					item1 = ActionUtil.findComponent(menu, type, tip);
					if (item1 != null) {
						method = dropdown.getClass().getDeclaredMethod(
								"getArrowButton");
						method.setAccessible(true);
						arrowButton = (JButton) method.invoke(dropdown);
						return item1;
					}

				} catch (NoSuchMethodException | SecurityException
						| IllegalAccessException | IllegalArgumentException
						| InvocationTargetException e) {
					// TODO Auto-generated catch block
					// 循环查找过程中会抛出未找到异常，不终止查找
					// e.printStackTrace();
				}
			}
		}
		if (item1 == null) {
			Application.getActiveApplication().getOutput().output("工具条按钮未找到。");
		}
		return item1;
	}
}
